package Lecture1;

/**
 * Created by Win7Pronly on 23.03.14.
 */
public class Graph {
    private Bag<Integer>[] adj;
    private int vNumber;
    private int eNumber;

    public Graph(int v) {
        adj = new Bag[v];
        this.vNumber = v;
        for (int i = 0; i < this.vNumber; i++) {
            this.adj[i] = new Bag<Integer>();
        }
    }


    public Iterable<Integer> adj(int v) {
        return adj[v];
    }

    void addEdge(Integer v, Integer w) {
        adj[v].add(w);
        adj[w].add(v);
        eNumber++;
    }

    int V() {
        return vNumber;
    }

    int E() {
        return eNumber;
    }







// Static methods block

    public static int degree(Graph g, int v) {
        int degree = 0;
        for (int i : g.adj(v)) {
            degree++;
        }
        return degree;
    }

    public static int maxDegree(Graph g) {
        int maxDegree = 0;
        for (int i = 0; i < g.V(); i++) {
            int degree = degree(g, i);
            if (degree > maxDegree) {
                maxDegree = degree;
            }
        }
        return maxDegree;
    }

    public static double averageDegree(Graph g) {
        int sumDegree = 0;
        for (int i = 0; i < g.V(); i++) {
            sumDegree += degree(g, i);
        }
        return sumDegree / g.V();
    }

    public static int numberOfSelfLoops(Graph g) {
        int selfLoopsNumber = 0;
        for (int v = 0; v < g.V(); v++) {
            for (int w : g.adj(v)) {
                if (w == v) selfLoopsNumber++;
            }
        }
        return selfLoopsNumber;
    }


}
